package com.wy.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.common.Constants;
import com.wy.common.R;
import com.wy.controller.dto.CustomerPetDTO;
import com.wy.controller.dto.UserDTO;
import com.wy.entity.Address;
import com.wy.entity.Pet;
import com.wy.entity.User;
import com.wy.service.IAddressService;
import com.wy.service.IPetService;
import com.wy.service.IUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.wy.service.ICustomerService;
import com.wy.entity.Customer;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-01-14
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private ICustomerService customerService;

    @Resource
    private IPetService petService;

    @Resource
    private IAddressService addressService;

    @Resource
    private IUserService userService;

    /****
     * 删除客户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        Customer customer = customerService.getById(id);
        customer.setDeleteStatus(0);
        customerService.updateById(customer);
        return R.success("删除成功");
    }
    /****
     * 新增或者更新
     * @return
     */
    @PostMapping
    public R save(@RequestBody Customer customer) {
        customerService.saveOrUpdate(customer);
        return R.success("");
    }

    /****
     * 客户分页
     * @param page
     * @param pageSize
     * @param username
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer page,
                      @RequestParam Integer pageSize,
                      @RequestParam String username) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(username)) {
            queryWrapper.like(Customer::getUsername, username);
        }
        queryWrapper.eq(Customer::getDeleteStatus,1);
        queryWrapper.orderByDesc(Customer::getId);
        Page<Customer> pageInfo = customerService.page(new Page<>(page, pageSize), queryWrapper);
        log.info("customer的pageInfo==>{}", pageInfo);
        return R.success(pageInfo);
    }

    /****
     * 获取客户列表
     * @return
     */
    @GetMapping
    public R findAll() {
        return R.success(customerService.list());
    }

    /****
     * 客户 获取医生列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getDoctor")
    public R getDoctorList(@RequestParam Integer page,
                           @RequestParam Integer pageSize) {
        Page<User> page1 = userService.page(new Page<>(page, pageSize), new LambdaQueryWrapper<User>().eq(User::getRole, "ROLE_DOCTOR"));
        return R.success(page1);
    }

    /****
     * 获取地址列表
     * @param id
     * @return
     */
    @GetMapping("/address/{id}")
    public R getAddressList(@PathVariable int id) {

        List<Address> addrList = addressService.getAddrList(id);
        return R.success(addrList);
    }

    /****
     * 前台客户登录
     * @param customer
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Customer customer) {
        if (StrUtil.isBlank(customer.getUsername()) || StrUtil.isBlank(customer.getPassword())) {
            return R.error(Constants.CODE_400, "参数错误");
        }
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", customer.getUsername());
        Customer customer1 = customerService.getOne(queryWrapper);
        if (customer1 == null) {
            return R.error(Constants.CODE_601, "用户名错误或不存在，请检查用户名");
        }
        if (!customer1.getPassword().equals(DigestUtils.md5DigestAsHex(customer.getPassword().getBytes()))) {
            return R.error(Constants.CODE_600, "密码错误");
        }
        return R.success("登录成功", customer1);
    }

    /****
     * 前台客户注册
     * @param customer
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody Customer customer) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getUsername, customer.getUsername());
        Customer one = customerService.getOne(queryWrapper);
        if (one != null) {
            return R.error(Constants.CODE_600, "用户名已存在，请更换用户名试试");
        }
        customer.setPassword(DigestUtils.md5DigestAsHex(customer.getPassword().getBytes()));
        boolean save = customerService.save(customer);
        if (!save) {
            return R.error(Constants.CODE_600, "系统错误");
        }
        return R.success("注册成功");
    }



    @PostMapping("/del/batch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        customerService.removeByIds(ids);
        return R.success("");
    }





}

